package cn.jbit.test.io;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * 源码学院只为培养优秀的bat程序员而生
 *
 * @author 学员张在成
 * @data 2021/6/27
 */
public class BioTestServer {

    public static void main(String[] args) throws IOException {

//        File file=new File("E:\\weijifen.txt");
//
//        FileInputStream  inputStream=new FileInputStream(file);
//
//        byte[] bytes=new byte[1024];
//        int read = inputStream.read(bytes);
//        System.out.println(read);
//        System.out.println(new String(bytes));

        ServerSocket serverSocket=new ServerSocket(8083);

        System.out.println("服务建立连接成功");
        while (true) {
            //接收连接
            Socket s = serverSocket.accept();
            System.out.println("接收客户端连接");
            //获取一个输入流
            InputStream inputStream = s.getInputStream();

            //定义一个字节数组接收数据
            byte[] bytes = new byte[1024];
            //定义一个length的变量，接收字节的长度
            int length = 0;
            //开始轮询读取数据
            while ((length = inputStream.read(bytes)) > 0) {
                System.out.println("字节的长度：" + length);
                System.out.println("receive client send data is：" + new String(bytes));
                //响应给客户端一条消息
                Scanner scanner = new Scanner(System.in);
                String next = scanner.next();
                OutputStream outputStream = s.getOutputStream();
                outputStream.write(next.getBytes());
            }


        }
    }
}
